home *** CD-ROM | disk | FTP | other *** search
- // mainfrm.cpp : implementation of the CMainFrame class
- //
-
- #include "stdafx.h"
- #include "zdemo.h"
- #include <zip.h>
- #include "mainfrm.h"
- #include <direct.h>
- #include "czip.h"
- #include "unzip.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // CMainFrame
-
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
-
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(IDM_SELECT, OnSelect)
- ON_COMMAND(IDM_ZIP, OnZipCommand)
- ON_COMMAND(IDM_UNZIP, OnUnzipCommand)
- ON_MESSAGE( PM_ZIP, OnZip )
- ON_MESSAGE( PM_UNZIP, OnUnzip )
- ON_MESSAGE( PM_VIEW, OnView )
- ON_MESSAGE( ZN_FILEZIPPED, OnZnFileZipped )
- ON_MESSAGE( ZN_ZIPPING, OnZnZipping )
- ON_MESSAGE( ZN_WRITING, OnWriting )
- ON_MESSAGE( ZN_COMPUTE, OnCompute )
- ON_MESSAGE( ZN_OPENFILE, OnOpenFile )
- ON_MESSAGE( ZN_EXPANDING, OnExpanding )
- ON_MESSAGE( ZN_CLOSEFILE, OnCloseFile )
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // arrays of IDs used to initialize control bars
-
- // toolbar buttons - IDs are command buttons
- static UINT BASED_CODE buttons[] =
- {
- // same order as in the bitmap 'toolbar.bmp'
- IDM_ZIP,
- IDM_UNZIP,
- };
-
- static UINT BASED_CODE indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- };
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
-
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
-
- CMainFrame::~CMainFrame()
- {
- }
-
-
- // ---------------------------------------------------------------------------
- // PreCreate the frame
- // ---------------------------------------------------------------------------
- BOOL CMainFrame::PreCreateWindow( CREATESTRUCT& cs )
- {
- cs.cy = 280;
- cs.cx = 500;
- cs.x = 80;
- cs.y = 80;
- cs.style = WS_VISIBLE | WS_MINIMIZEBOX | WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU;
- return CFrameWnd::PreCreateWindow( cs );
- }
-
-
- // ---------------------------------------------------------------------------
- // Create the frame
- // ---------------------------------------------------------------------------
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!m_wndToolBar.Create(this) ||
- !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
- !m_wndToolBar.SetButtons(buttons,
- sizeof(buttons)/sizeof(UINT)))
- {
- TRACE("Failed to create toolbar\n");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE("Failed to create status bar\n");
- return -1; // fail to create
- }
- return 0;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
-
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
-
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
-
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
-
-
- /////////////////////////////////////////////////////////////////
- // WM_COMMAND ( IDM_SELECT )
- // Select the ZIP file
- void CMainFrame::OnSelect()
- {
- // Show an open dialog box to let the user choose an existing zip file or to create it
- CString OldPath, NewPath, ActualPath;
- _getcwd( OldPath.GetBuffer( 160), 158 );
- OldPath.ReleaseBuffer();
- if ( CurrentZipFile != "" ) // get the zip path if exist
- ActualPath = GetPathFromFileName( CurrentZipFile );
- else
- ActualPath = "";
- if ( ActualPath.GetLength() > 3 ) // delete the slash
- _chdir( ActualPath.Left(ActualPath.GetLength()-1) );
- else
- _chdir( ActualPath );
- CFileDialog FileDialogBox( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_NOVALIDATE | OFN_PATHMUSTEXIST ,"Zip file (*.ZIP) |*.ZIP|All files (*.*)|*.*||" );
- if ( FileDialogBox.DoModal() == IDOK )
- {
- _getcwd( CurrentZipFile.GetBuffer( 180), 178 );
- CurrentZipFile.ReleaseBuffer();
- if ( CurrentZipFile.GetLength() > 3 )
- CurrentZipFile += "\\";
- CurrentZipFile += FileDialogBox.GetPathName();
- }
- if ( OldPath.GetLength() > 4 )
- OldPath = OldPath.Left( OldPath.GetLength()-1);
- GetActiveView()->SetDlgItemText(IDC_FILE, CurrentZipFile );
- GetActiveView()->SetDlgItemText(IDC_TEXT, "" );
- GetActiveView()->SetDlgItemText(IDC_OPERATION, "" );
- _chdir( OldPath );
- }
-
-
-
- /////////////////////////////////////////////////////////////////
- // WM_COMMAND ( IDM_ZIP )
- // Call ZIP
- void CMainFrame::OnZipCommand()
- {
- // Call the CCZip dialog box to perform the zip process
- CCzip ZipDialogBox(this);
- ZipDialogBox.DoModal();
- }
-
-
- /////////////////////////////////////////////////////////////////
- // WM_COMMAND ( IDM_UNZIP )
- // Call UNZIP
- void CMainFrame::OnUnzipCommand()
- {
- // Call the CUnZip dialog box to perform the unzip process
- CUnZip UnzipDialogBox(this);
- UnzipDialogBox.DoModal();
- }
-
-
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Private messages to pilote the process
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // Start the Zip process ( PM_ZIP )
- afx_msg LRESULT CMainFrame::OnZip( WPARAM wParam, LPARAM lParam )
- {
- int Result;
- Result = AddFileToZip( CurrentZipFile, sMask, iOverwrite, bPath,bRecurse,m_hWnd );
- // Depending on the result, we set the text in the main window
- switch ( Result ) // I have some troubles regarding constants with my compiler...
- {
- case 1: // warning ( ZERROR_WARNING )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Done ( but warnings )" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "ok but warnings" );
- case 2: // file error ( ZERROR_DESTFILE )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "File error" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Source file or ZIP file\nmaybe dammaged!" );
- case 3: // internal error ( ZERROR_INTERNAL )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Internal error" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Internal error.\nMaybe Zip file not generated!" );
- case 4: // file error ( ZERROR_FORMAT )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Not a zip file" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Not a zip file.\nMaybe zip file corrupted!" );
- case 5: // file error ( ZERROR_NOMEM )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Not enough memory" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Not enough memory to zip!\nSerious error!\nPlease use a smaller ZIP file!" );
- case 6: // file error ( ZERROR_NOFILE )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "No file or nothing to do!" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "No file or nothing to do!" );
- case 7: // file error ( ZERROR_NODLL )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Can' t find ZDLL12B.DLL" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Please put ZDLL12B.DLL in your current directory.\nMaybe this DLL is corrupted." );
- default:// ok ( ZERROR_OK )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Done" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "ok" );
- }
- m_wndStatusBar.SetWindowText( "Ready" );
- return 0L;
- }
-
-
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // Start the Unzip process (PM_UNZIP )
- afx_msg LRESULT CMainFrame::OnUnzip( WPARAM wParam, LPARAM lParam )
- {
- int Result;
- SetReceivingWindow( m_hWnd ); // to receive all notification messages during unzipping
- SetBackgroundMode( TRUE ); // to activate the multitask mode
- Result = ExtractZipFiles( CurrentZipFile, sMask, "" /* DestDir is always WINDOWS/TEST */, iOverwrite, bPath );
- // Depending on the result, we set the text in the main window
- switch ( Result ) // I have some troubles regarding constants with my compiler...
- {
- case 1: // internal error ( ZEXTRACT_INTERNALERROR )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Internal error" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Internal error.\nMaybe files not created!" );
- case 2: // file not found ( ZEXTRACT_FILENOTFOUND )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "File not found" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Source file missing.\nPlease choose existing files to unzip!" );
- case 3: // bad format ( ZEXTRACT_CORRUPTED )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Zip file format error" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Zip file format error.\nMaybe Zip file is dammaged!" );
- case 4: // no file ( ZEXTRACT_EMPTY )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Nothing to do!" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Nothing to do: no files!" );
- case 5: // fatal error ( ZEXTRACT_ERRORINZIPFILE )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Fatal error" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Fatal error!\nMaybe zip file is corrupted!" );
- default:// ok ( ZERROR_OK )
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Unzipping is done" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "ok" );
- }
- m_wndStatusBar.SetWindowText( "Ready" );
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // Start the View process ( PM_VIEW )
- afx_msg LRESULT CMainFrame::OnView( WPARAM wParam, LPARAM lParam )
- {
- SetReceivingWindow( m_hWnd ); // to receive all notification messages during unzipping
- SetBackgroundMode( TRUE ); // to activate the multitask mode
- if ( !ViewFileFromZip( CurrentZipFile, sMask, FALSE )) // view one or several files in a Zip
- MessageBox( "Maybe not all files can be viewed...", "Zip Studio", MB_OK | MB_ICONEXCLAMATION );
- m_wndStatusBar.SetWindowText( "Ready" );
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Notifications messages treatment ( ZN_... )
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is completly zipped ( ZN_FILEZIPPED ) [ZIP]
- afx_msg LRESULT CMainFrame::OnZnFileZipped( WPARAM wParam, LPARAM lParam )
- {
- CString csOperation, csStatus, csDetail;
- wsprintf( csStatus.GetBuffer(200), "The file %s is zipped!", (LPSTR)lParam );
- csStatus.ReleaseBuffer();
- wsprintf( csOperation.GetBuffer(200), "%s zipped!", (LPSTR)lParam );
- csOperation.ReleaseBuffer();
- wsprintf( csDetail.GetBuffer(300), "%s is cuccessfully added to\n%s.\nCompression rate is %i %%.",
- (LPSTR)lParam, CurrentZipFile, (int)wParam );
- csDetail.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csOperation );
- GetActiveView()->SetDlgItemText( IDC_TEXT, csDetail );
- m_wndStatusBar.SetWindowText( csStatus );
- return 0L;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is processing ( ZN_ZIPPING ) [ZIP]
- afx_msg LRESULT CMainFrame::OnZnZipping( WPARAM wParam, LPARAM lParam )
- {
- CString csOperation, csStatus;
- wsprintf ( csStatus.GetBuffer(180), "Compressed size: %i Ko.", (int)wParam );
- csStatus.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csStatus );
- if ( wParam < 4 ) // at the beginning...
- {
- wsprintf ( csOperation.GetBuffer(180), "%s in process!", (LPSTR)lParam );
- csOperation.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_TEXT, csOperation );
- m_wndStatusBar.SetWindowText( csOperation );
- }
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // The zip file is complete ( ZN_WRITING ) [ZIP]
- afx_msg LRESULT CMainFrame::OnWriting( WPARAM wParam, LPARAM lParam )
- {
- CString csOperation, csStatus;
- wsprintf( csStatus.GetBuffer(200), "%s successfully zipped! Please wait...", (LPSTR)lParam );
- csStatus.ReleaseBuffer();
- wsprintf( csOperation.GetBuffer(180), "%s zipped!", (LPSTR)lParam );
- csOperation.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csOperation );
- GetActiveView()->SetDlgItemText( IDC_TEXT, csOperation );
- m_wndStatusBar.SetWindowText( csStatus );
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is processing ( ZN_COMPUTE ) [ZIP]
- afx_msg LRESULT CMainFrame::OnCompute( WPARAM wParam, LPARAM lParam )
- {
- CString csStatus, CTotal;
- _itoa( (unsigned)wParam, CTotal.GetBuffer(10), 10 );
- CTotal.ReleaseBuffer();
- wsprintf( csStatus.GetBuffer(200), "%s contains " , CurrentZipFile ); // bug?? with %i and (int)wParam
- csStatus.ReleaseBuffer();
- csStatus += CTotal;
- csStatus += " files.";
- MessageBox( csStatus );
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csStatus );
- GetActiveView()->SetDlgItemText( IDC_TEXT, csStatus );
- m_wndStatusBar.SetWindowText( csStatus );
- // add a message box if treatment require a long time ( to prevent the user to reboot !!! )
- if ( wParam > 500 )
- {
- if ( wParam > 1000 )
- MessageBox( "There are many files in this Zip file.\nComputing this file will require few minutes.", "Zip Studio", MB_OK | MB_ICONEXCLAMATION );
- else
- MessageBox( "There are many files in this Zip file.\nComputing this file will require few seconds.", "Zip Studio", MB_OK );
- }
- return 0L;
- }
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is opened for unzip ( ZN_OPENFILE ) [UNZIP]
- afx_msg LRESULT CMainFrame::OnOpenFile( WPARAM wParam, LPARAM lParam )
- {
- CString csStatus, csDetail, csMessage;
- if ( !wParam )
- {
- wsprintf( csMessage.GetBuffer(200), "Can' t open %s!", (LPSTR)lParam );
- csMessage.ReleaseBuffer();
- MessageBox( csMessage, "Zip Studio", MB_OK | MB_ICONSTOP );
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Fatal error!" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Fatal error!" );
- m_wndStatusBar.SetWindowText( "Fatal error!" );
- return 0L;
- }
- wsprintf( csStatus.GetBuffer(200), "%s successfully opened.", (LPSTR)lParam );
- csStatus.ReleaseBuffer();
- wsprintf( csDetail.GetBuffer(300), "%s from %s\nis opened.", (LPSTR)lParam, CurrentZipFile );
- csDetail.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csStatus );
- GetActiveView()->SetDlgItemText( IDC_TEXT, csDetail );
- m_wndStatusBar.SetWindowText( csStatus );
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is in process ( ZN_EXPANDING ) [UNZIP]
- afx_msg LRESULT CMainFrame::OnExpanding( WPARAM wParam, LPARAM lParam )
- {
- CString csStatus, csDetail;
- wsprintf( csStatus.GetBuffer(200), "%s is in process.- %i %% filled.", (LPSTR)lParam, (int)wParam );
- csStatus.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csStatus );
- if ( wParam < 4 ) // at the beginning
- {
- wsprintf( csDetail.GetBuffer(300), "%s from %s\nis unzipping...", (LPSTR)lParam, CurrentZipFile );
- csDetail.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_TEXT, csDetail );
- m_wndStatusBar.SetWindowText( "Unzipping..." );
- }
- return 0L;
- }
-
-
-
- /////////////////////////////////////////////////////////////////////////////////
- // A file is unzipped ( ZN_CLOSEFILE ) [UNZIP]
- afx_msg LRESULT CMainFrame::OnCloseFile( WPARAM wParam, LPARAM lParam )
- {
- CString csOperation, csStatus, csDetail, csMessage;
- if ( !wParam )
- {
- wsprintf( csMessage.GetBuffer(200), "Can' t unzip %s!", (LPSTR)lParam );
- csMessage.ReleaseBuffer();
- MessageBox( csMessage, "Zip Studio", MB_OK | MB_ICONSTOP );
- GetActiveView()->SetDlgItemText( IDC_OPERATION, "Fatal error!" );
- GetActiveView()->SetDlgItemText( IDC_TEXT, "Fatal error!" );
- m_wndStatusBar.SetWindowText( "Fatal error!" );
- return 0L;
- }
- wsprintf( csStatus.GetBuffer(200), "%s is successfully unzipped.", (LPSTR)lParam );
- csStatus.ReleaseBuffer();
- wsprintf( csDetail.GetBuffer(300), "%s from %s\nis successfully unzipped.", (LPSTR)lParam, CurrentZipFile );
- csDetail.ReleaseBuffer();
- GetActiveView()->SetDlgItemText( IDC_OPERATION, csStatus );
- GetActiveView()->SetDlgItemText( IDC_TEXT, csDetail );
- m_wndStatusBar.SetWindowText( csStatus );
- return 0L;
- }
-